package org.grow.wsing.stomp;

import com.sun.security.auth.UserPrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

import java.net.URI;
import java.security.Principal;
import java.util.Arrays;
import java.util.Map;

/**
 * @Author: xwg
 * @CreateDate: 2025/10/20
 */
@Component
//        implements HandshakeHandler
public class AuthHandShake extends DefaultHandshakeHandler {

//    @Autowired
//    private JwtServer JwtUtil
    @Override
    protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler,
                                      Map<String, Object> attributes) {
//        System.out.println(request);
        System.out.println(request.getHeaders());
        System.out.println(request.getURI());
//        System.out.println(attributes);
//        http://localhost:8080/chatting?token=ey2.jwt.zhaoliu
        URI uri = request.getURI();
//        token=ey2.jwt.zhaoliu
        String query = uri.getQuery();
        String[] a = query.split("=");
//        System.out.println(Arrays.toString(a));
        String[] b = a[1].split("\\.");
//        System.out.println(Arrays.toString(b));
        String uid = b[2];
        System.out.println("解析jwt完毕 当前登录人"+uid);
        UserPrincipal userPrincipal = new UserPrincipal(uid);

        return userPrincipal;


    }
}
